package com.yan.club.sys.control;

import com.yan.club.auth.model.AuthUser;
import com.yan.club.auth.model.LoginUser;
import com.yan.club.result.Result;
import com.yan.club.sys.model.SysUser;
import com.yan.club.sys.service.SysUserService;
import com.yan.club.sys.vo.ProfileVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.yan.club.auth.utils.SecurityUtil.*;
import static com.yan.club.result.ResultCode.INTERNAL_SERVER_ERROR;

/**
 * @author 彦
 * @since 2024/2/26 9:55
 * 个人信息
 */
@RestController
@RequestMapping("/sys/user/profile")
public class SysProfileController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 个人信息
     */
    @GetMapping
    public Result<ProfileVo> profile() {
        String username = getUser().getUserName();
        SysUser user = sysUserService.selectUserByUserName(username);
        ProfileVo profileVo = new ProfileVo();
        profileVo.setUser(user);

        String roleGroup = sysUserService.selectUserRoleGroup(username);
        profileVo.setRoleGroup(roleGroup);
        return Result.success(profileVo);
    }

    /**
     * 修改用户
     */
    @PutMapping
    public Result<String> updateProfile(@RequestBody SysUser user) {
        LoginUser loginUser = getLoginUser();
        AuthUser authUser = loginUser.getUser();
        SysUser currentUser = new SysUser();

        BeanUtils.copyProperties(authUser, currentUser);
        currentUser.setName(user.getName());
        currentUser.setEmail(user.getEmail());
        currentUser.setPhonenumber(user.getPhonenumber());
        currentUser.setSex(user.getSex());
        if (StringUtils.isNotEmpty(user.getPhonenumber()) && sysUserService.checkPhoneUnique(currentUser)) {
            return Result.error(INTERNAL_SERVER_ERROR.code,
                    "修改用户'" + loginUser.getUsername() + "'失败，手机号码已存在");
        }
        if (StringUtils.isNotEmpty(user.getEmail()) && sysUserService.checkEmailUnique(currentUser)) {
            return Result.error(INTERNAL_SERVER_ERROR.code,
                    "修改用户'" + loginUser.getUsername() + "'失败，邮箱账号已存在");
        }
        if (sysUserService.updateUserProfile(currentUser) > 0) {
            // 更新缓存用户信息
//            tokenService.setLoginUser(loginUser);
            return Result.success();
        }
        return Result.error(INTERNAL_SERVER_ERROR.code, "修改个人信息异常，请联系管理员");
    }

    /**
     * 重置密码
     */
    @PutMapping("/updatePwd")
    public Result<String> updatePwd(String oldPassword, String newPassword) {
        String username = getUser().getUserName();
        SysUser user = sysUserService.selectUserByUserName(username);
        String password = user.getPassword();

        if (!matchesPassword(oldPassword, password)) {
            return Result.error(INTERNAL_SERVER_ERROR.code, "修改密码失败，旧密码错误");
        }
        if (matchesPassword(newPassword, password)) {
            return Result.error(INTERNAL_SERVER_ERROR.code, "新密码不能与旧密码相同");
        }
        newPassword = encryptPassword(newPassword);

        if (sysUserService.resetUserPwd(username, newPassword) > 0) {
            // 更新缓存用户密码
//            LoginUser loginUser = SecurityUtils.getLoginUser();
//            loginUser.getUser().setPassword(newPassword);
//            tokenService.setLoginUser(loginUser);
            return Result.success();
        }

        return Result.error(INTERNAL_SERVER_ERROR.code, "修改密码异常，请联系管理员");
    }
}
